/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ModuloConfiguracion;

import Acceso.Security;
import Modelo.DAL.Piso_DAO;
import Modelo.Piso;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

/**
 *
 * @author alb3rto
 */
public class AgregarPisoEdificioAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    private static final String[] ALLOWED = {"Administrador"};

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {

        if (!Security.validateSession(request, ALLOWED)) {
            Security.privilegeError(response);
            return mapping.findForward("errorPriv");
        }

        try {
            ArrayList allows = new ArrayList();
            allows.add("image/jpeg");
            allows.add("image/gif");
            allows.add("image/png");

            AgregarPisoEdificioForm formBean = (AgregarPisoEdificioForm) form;

            String nombreEdificio = formBean.getEdificio();
            String sede = formBean.getSede();
            String piso = formBean.getPiso();
            FormFile plano = formBean.getFormFile();

            //Validacion del tipo de archivo
            String contentType = plano.getContentType();
            boolean allow = allows.contains(contentType);
            System.out.println(contentType + " - " + allow);
            if (!allow) {
                formBean.setError("Error: No se ha podido agregar el plano. Formato de archivo no permitido.");
                return mapping.findForward(FAILURE);
            }

            byte[] fileData = plano.getFileData();
            String filePath = getServlet().getServletContext().getRealPath("/style/images/Mapas/Planos/");
            File archivo = new File(filePath, plano.getFileName());
            FileOutputStream fos = new FileOutputStream(archivo);
            fos.write(fileData);
            fos.flush();
            fos.close();

            String rutaPlano = archivo.getAbsolutePath();
            System.out.println(">>>>>>>>>>>>>>>" + piso + ": " + nombreEdificio + ": " + sede + ": " + rutaPlano);

            Piso ps = new Piso(piso, nombreEdificio, sede, rutaPlano);
            if (!Piso_DAO.insertPiso(ps)) {
                formBean.setError("Error: No se ha podido agregar la ruta de escape. Por favor intente nuevamente");
                return mapping.findForward(FAILURE);
            }

            return mapping.findForward(SUCCESS);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return mapping.findForward(FAILURE);
        }
    }
}
